<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var decrypt = function (code, k) {
      let n = code.length, ans = Array(n).fill(0), sign = k < 0
      if (k === 0) return ans
      if (k < 0) {
        code.reverse()
        k = -k
      }

      for (let i = 0; i < n; i++) {
        for (let j = 1; j <= k; j++) {
          ans[i] += code[(i + j) % n]
        }
      }

      return sign ? ans.reverse() : ans
    };
    console.log(decrypt([1, 2, 3, 4], 0));
  </script>
</body>

</html>